package in.iqing.view.widget;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by ZuoShu on 15/7/4.
 */
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
    private int spanCount;
    private int verticalSpacing;
    private int horizontalSpacing;

    public SpacesItemDecoration(int spanCount, int horizontalSpacing, int verticalSpacing) {
        this.spanCount = spanCount;
        this.horizontalSpacing = horizontalSpacing;
        this.verticalSpacing = verticalSpacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        int column = position % spanCount;

        outRect.left = column * horizontalSpacing / spanCount;
        outRect.right = horizontalSpacing - (column + 1) * horizontalSpacing / spanCount;
        if (position >= spanCount) {
            outRect.top = verticalSpacing;
        }
    }
}